package com.ruoyi.danny.controller;


import com.ruoyi.common.core.web.domain.AjaxResult;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/flow2")
public class Activiti7gController {
    private static final Logger log = LoggerFactory.getLogger(Activiti7gController.class);
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private ProcessRuntime processRuntime;
    @Autowired
    private RuntimeService runtimeService;


    /**
     * 1.流程部署/创建流程
     */
    @GetMapping("/addDeployment")
    public AjaxResult test3() {
        // 1.获取ProcessEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        // 2.完成流程的部署操作 需要通过RepositoryService来完成
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //3.完成部署操作
        // 第一种方式
        //Deployment deploy = repositoryService.createDeployment()
        //        .addClasspathResource("flow/test1.bpmn20.xml")
        //        .name("第一个流程")
        //        .deploy();
        // 第二种方式
        // 涉及 act_re_deployment:流程部署表;act_re_procdef:流程定义表
        Deployment deploy = repositoryService.createDeployment()
                .addString("部署资源名称.bpmn", "demoXml") // act_re_procdef.RESOURCE_NAME_  流程部署表
                .name("部署名称") //act_re_deployment.name 流程定义表
                .deploy();
        log.info("Deployment:{}", deploy);
        //   Deployment:DeploymentEntity[id=40ffc43c-6135-11f0-826f-9c2dcd6d427f, name=1部署名称]
        return AjaxResult.success(deploy);
    }

    /**
     * 2.流程部署查询
     */
    @GetMapping("/listDeployment")
    public AjaxResult test4() {
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = engine.getRepositoryService();
        // 查询有哪些部署的流程--》查询相关的流程定义信息
        // repositoryService.createDeploymentQuery() 查询流程部署的相关信息
        // repositoryService.createProcessDefinitionQuery() 查询部署的流程的相关的定义
        //List<Deployment> list = repositoryService.createDeploymentQuery().list(); // 查询所有的部署信息
        //log.info("查询相关的流程定义信息:{}", list);
        // 查询所有的部署信息:[DeploymentEntity[id=15bae085-6128-11f0-a38a-9c2dcd6d427f, name=部署名称], DeploymentEntity[id=51ab6871-6060-11f0-acd8-9c2dcd6d427f, name=null], DeploymentEntity[id=5550c1f5-6127-11f0-ae46-9c2dcd6d427f, name=null], DeploymentEntity[id=95183ad0-6126-11f0-a974-9c2dcd6d427f, name=第一个流程], DeploymentEntity[id=ac375c00-5c9f-11f0-bc27-9c2dcd6d427f, name=null], DeploymentEntity[id=b3ca88ef-5c96-11f0-aca6-9c2dcd6d427f, name=SpringAutoDeployment], DeploymentEntity[id=caa671c1-6132-11f0-8c50-9c2dcd6d427f, name=1部署名称], DeploymentEntity[id=cf52f053-5c9f-11f0-bc27-9c2dcd6d427f, name=null], DeploymentEntity[id=d04ed013-6127-11f0-a696-9c2dcd6d427f, name=部署名称], DeploymentEntity[id=d865a1b6-5c9f-11f0-bc27-9c2dcd6d427f, name=null], DeploymentEntity[id=f7687772-6128-11f0-841f-9c2dcd6d427f, name=部署名称]]


        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery()
                .orderByProcessDefinitionId()
                .orderByProcessDefinitionVersion()
                .desc();
        //processDefinitionQuery.processDefinitionNameLike("%" + processDefinition.getName() + "%");


        // 在这里添加过滤条件实现过滤
        List<ProcessDefinition> processDefinitions = processDefinitionQuery.listPage(0, 10);
        //List<ProcessDefinition> processDefinitions = processDefinitionQuery.list();
        log.info("查询部署的流程的相关的定义:{}", processDefinitions);
        // 12:19:41.875 [main] INFO  MyServiceTest - [test4,243] - 查询部署的流程的相关的定义:[ProcessDefinitionEntity[Process_1:1:51b0bfa3-6060-11f0-acd8-9c2dcd6d427f], ProcessDefinitionEntity[leave88:1:d86a83b8-5c9f-11f0-bc27-9c2dcd6d427f], ProcessDefinitionEntity[leave:1:b409b4d2-5c96-11f0-aca6-9c2dcd6d427f], ProcessDefinitionEntity[leave:2:ac534872-5c9f-11f0-bc27-9c2dcd6d427f], ProcessDefinitionEntity[leave:3:cf5c8d45-5c9f-11f0-bc27-9c2dcd6d427f], ProcessDefinitionEntity[leave:4:952b2692-6126-11f0-a974-9c2dcd6d427f], ProcessDefinitionEntity[leave:5:556115a7-6127-11f0-ae46-9c2dcd6d427f], ProcessDefinitionEntity[leave:6:d068e7c5-6127-11f0-a696-9c2dcd6d427f], ProcessDefinitionEntity[leave:7:15d51f47-6128-11f0-a38a-9c2dcd6d427f], ProcessDefinitionEntity[leave:8:f78156a4-6128-11f0-841f-9c2dcd6d427f], ProcessDefinitionEntity[leave:9:cab4f0b3-6132-11f0-8c50-9c2dcd6d427f], ProcessDefinitionEntity[reim_at10_10:1:b40c25d3-5c96-11f0-aca6-9c2dcd6d427f]]

        return AjaxResult.success(processDefinitions);
    }

    /**
     * 3.发起一个流程,通过变量
     */
    @GetMapping("/addProcess")
    public AjaxResult test5() {
        //ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        //// 发起流程 需要通过 runtimeService来实现
        //RuntimeService runtimeService = engine.getRuntimeService();
        // 通过流程定义ID来启动流程 返回的是流程实例对象
        Map< String, Object > map = new HashMap< >();
        map.put("deptLeader", "1234");
        ProcessInstance processInstance = runtimeService
                .startProcessInstanceById("leave:1:b409b4d2-5c96-11f0-aca6-9c2dcd6d427f",map);
        log.info("发起一个流程,第一种方式:{}", processInstance);//  act_ru_task  :ProcessInstance[4c7f41f1-6133-11f0-b890-9c2dcd6d427f]





        //org.activiti.api.process.model.ProcessInstance processInstance = processRuntime.start(
        //        ProcessPayloadBuilder
        //                .start()
        //                .withProcessDefinitionKey("leave")
        //                .withName("标题测试")
        //                .withBusinessKey(""+System.currentTimeMillis())
        //                .withVariable("deptLeader", "join")
        //                .build()
        //);

        return AjaxResult.success(processInstance);
    }

    /**
     * 查询发起的流程,待办查询
     */
    @GetMapping("/listProcess")
    public AjaxResult test6() {
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 待办查询 执行中的任务处理通过 TaskService来实现
        TaskService taskService = engine.getTaskService();
        // Task 对象对应的其实就是 act_ru_task 这张表的记录
        List<Task> list = taskService.createTaskQuery().taskAssignee("zhangsan1").list(); //act_ru_task.ASSIGNEE_

        //task = taskService.createTaskQuery()
        //        .processDefinitionKey("OwnerEvection") // 流程的key
        //        .taskAssignee("wangwu")   // 要查询的负责人
        //        .singleResult();// 因为可能有多个待办  所以是一个集合
        //taskService.complete(task.getId());

        if (list != null && list.size() > 0) {
            for (Task task : list) {
                System.out.println(task.getId());
                System.out.println(task.getName());
                System.out.println(task.getAssignee());
            }
        } else {
            System.out.println("当前没有待办任务");
        }

        return AjaxResult.success(list);
    }

    /**
     * 任务审批
     */
    @GetMapping("/auditProcess")
    public AjaxResult test7() {
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 做任务申请 也需要通过 TaskService 来实现
        TaskService taskService = engine.getTaskService();
        // 根据当前登录用户查询出对应的待办信息
        List<Task> list = taskService.createTaskQuery().taskAssignee("zhangsan2").list();
        if (list != null && list.size() > 0) {
            for (Task task : list) {
                // 做对应的任务审批处理
                taskService.complete(task.getId());
            }
        }
        // 完成任务
        // taskService.complete("2505");
        return AjaxResult.success(list);
    }
}
